import { _decorator, game } from "cc";
import BaseDialog from "./BaseDialog";
import APIHelper from "../../api/APIHelper";
const {ccclass,property} = _decorator
@ccclass
export default class VideoDialog extends BaseDialog {
    _player=null;
	private _currentTime=0
	private _isPlayCompleted=false
	private bt_close:HTMLDivElement
	onLoad(){
		const div=document.createElement('div')
		div.style.position="absolute"
		div.style.top="10px"
		div.style.right="10px"
		div.style.width="48px"
		div.style.height="48px"
		div.style.backgroundImage="url(关闭@2x.png)"
		div.style.zIndex='10000'
		div.onclick=()=>{
			this.xBtn()
			//div.parentElement.removeChild(div)
		}
		this.bt_close=div
	}
	play(id,lastTime=0){
		//const self=this
		const player = document.getElementById("player-container-id");
		const doc=document.documentElement
		const cw=doc.clientWidth,ch=doc.clientHeight
		//console.log(doc.clientWidth,doc.clientHeight)
		if(player){
			player.style.display = "block"
			//player.style.width =gameDiv.style.width
			//player.style.height =gameDiv.style.height
			player.style.width=100+"%"
			player.style.height=100+"%"
			this.bt_close.style.display="block"
			player.parentElement.appendChild(this.bt_close)
		}
		const api=APIHelper.getInstance()
		if(window.TCPlayer){
			api.superPlayAddIntegral(id).then(data=>{
				console.log(data)
				let flag=true
				this._isPlayCompleted=false
				this._player =new TCPlayer('player-container-id', { // player-container-id 为播放器容器 ID，必须与 html 中一致
					fileID: data.fileID, // 请传入需要播放的视频 fileID（必须）
					appID: data.appID, // 请传入点播账号的 appID（必须）
					//私有加密播放需填写 psign， psign 即超级播放器签名，签名介绍和生成方式参见链接：https://cloud.tencent.com/document/product/266/42436
					psign:data.sign,
					hlsConfig:{},
					autoplay:true
					//视频播放完成后的回调函数
				},(callback)=>{
					//const body=document.body
					//console.log(body.clientWidth,body.clientHeight)
					//const gameDiv=document.getElementById("GameDiv")
					console.log(doc.clientWidth,doc.clientHeight)
					//playerHtml.style.width =gameDiv.style.width
					//playerHtml.style.height =100+"%";
					game.pause()
					//console.log(callback)
					//this._player.currentTime(lastTime)
					this._player.on('timeupdate',()=>{
						//console.log("当前播放时间: ", this._player.currentTime())
						this._currentTime=this._player.currentTime()
					})
					this._player.on('ended',()=>{
						this._isPlayCompleted=true
						console.log("播放结束")
						this.successCallback({isCompleted:this._isPlayCompleted?1:0,lastTime:this._currentTime})
					})
					this._player.on('canplay',()=>{
						//console.log("可以播放了")
						if(flag){
							this._player.currentTime(lastTime)
							flag=false
						}
						
					})
					this._player.on('fullscreenchange',(event)=>{
						console.log("fullscreenchange")
						//console.log(body.clientWidth,body.clientHeight)
						console.log(doc.clientWidth,doc.clientHeight)
						console.log(wx)
						this.scheduleOnce(()=>{
							if(doc.clientWidth==cw){
								//doc.style.height=ch+'px'
								//@ts-ignore
								wx.miniProgram.postMessage({ data: { action: 'exitFullScreen' } });
							}
						},0.2)
					})
				});
				//console.log(this._player)
				this._player.currentTime(lastTime)
			})
		}
	}

	xBtn() {
		const video_element = document.getElementById("player-container-id") as HTMLVideoElement;
		if(video_element){
			//console.log(this._currentTime)
			video_element.style.display = "none";
		}
		
		if(this._player){
			//this._player.off('timeupdate',this.timeUpdateCallback)
			this._player.dispose();
			let player = document.createElement("video");
			player.id = "player-container-id";
			// let window=view.getCanvasSize ();
			// console.log(window)
			player.style.width=100+"%";
			player.style.height=60+"%";
			player.preload="auto";
			player.playsInline=true;
			document.body.appendChild(player);
		}
		game.resume();
		this.onClose();
		if(!this._isPlayCompleted){
			this.closeCallback&&this.closeCallback({isCompleted:this._isPlayCompleted?1:0,lastTime:this._currentTime})
		}
		this.bt_close.style.display="none"
	}
}